(defvar ab-config/perltidy-invocation
  (concat ab-config/perl-invocation
          " "
          ab-config/perltidy-path
          " -q "))

(setq cperl-hairy t)
(custom-set-variables
 '(cperl-close-paren-offset 4)
 '(cperl-continued-statement-offset 4)
 '(cperl-indent-level 4)
 '(cperl-indent-parens-as-block t)
 '(cperl-tab-always-indent t)
 '(dired-listing-switches "-lR"))

(defun perltidy-region ()
    "Run perltidy on the current region."
    (interactive)
    (save-excursion
      (shell-command-on-region (point) (mark)
                               ab-config/perltidy-invocation nil t)
      (cperl-mode)))

(defun perltidy-all ()
    "Run perltidy on the current region."
    (interactive)
    (let ((p (point)))
      (save-excursion
        (shell-command-on-region (point-min) (point-max)
                                 ab-config/perltidy-invocation nil t)
        )
      (goto-char p)
      (cperl-mode)))

(add-hook 'cperl-mode-hook 
          (lambda ()
            (local-set-key "\M-T" `perltidy-all) 
            (local-set-key "\M-t" `perltidy-region) 
            (setq show-trailing-whitespace t)
            ))
